<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;


class BizProjectCategoryModel extends BizProjectBaseModel
{

    const _TABLE_NAME = 'biz_project_category';

    use BizProjectTrait;

    public static function topList(): array
    {
        $list = self::dataSelect(['pid' => 0] + self::statusCondition());
        return $list ? array_column($list, 'name', 'id') : [];
    }

    /**
     * $group = [
     *        [
     *        'label' => 'xxxx',
     *        'options' => [
     *            1 => 'foo',
     *            2 => 'bar',
     *            ...
     *        ],
     *        ...
     *     ]
    */
    public static function adminNameGroupOptions(): array
    {
        $group = [];
        $list = self::dataSelect(self::statusCondition());
        if ($list) {
            $options = [];
            foreach ($list as $v) {
                if ($v['pid'] == 0) {
                    $options[$v['id']] = [];
                    $group[] = [
                        'label' => $v['name'],
                        'options' => &$options[$v['id']]
                    ];
                } else {
                    $options[$v['pid']][$v['id']] = $v['name'];
                }
            }
        }
        return $group;
    }

    public static function adminNameLevelOptions(): array
    {
        $response = [];
        $group = self::adminNameGroupOptions();
        $list = self::dataSelect(self::statusCondition());
        if ($group) {
            foreach ($group as $v) {
                foreach ($v['options'] as $id => $name) {
                    $response[$id] = $v['label'] . " - " . $name;
                }
            }
        }
        return $response;
    }
}
